package com.ruoyi.ejkgl.ejkck.service.impl;

import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.ejkgl.ejkck.domain.Ejkck;
import com.ruoyi.ejkgl.ejkck.domain.EjkckCkmx;
import com.ruoyi.ejkgl.ejkck.mapper.EjkckMapper;
import com.ruoyi.ejkgl.ejkck.service.IEjkckService;
import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 二级库出库Service业务层处理
 *
 * @author huacai
 * @date 2024-12-11
 */
@Service
public class EjkckServiceImpl implements IEjkckService {
    @Resource
    private EjkckMapper ejkckMapper;

    @Resource
    private SqlSessionFactory sqlSessionFactory;

    /**
     * 查询二级库出库
     *
     * @param ejkckId 二级库出库主键
     * @return 二级库出库
     */
    @Override
    public Ejkck selectEjkckByEjkckId(String ejkckId) {
        return ejkckMapper.selectEjkckByEjkckId(ejkckId);
    }

    /**
     * 查询二级库出库列表
     *
     * @param ejkck 二级库出库
     * @return 二级库出库
     */
    @Override
    public List<Ejkck> selectEjkckList(Ejkck ejkck) {
        return ejkckMapper.selectEjkckList(ejkck);
    }

    /**
     * 新增二级库出库
     *
     * @param ejkck 二级库出库
     * @return 结果
     */
    @Transactional
    @Override
    public int insertEjkck(Ejkck ejkck) {
        ejkck.setCreateTime(DateUtils.getNowDate());
        // 获取当前的用户
        LoginUser loginUser = SecurityUtils.getLoginUser();
        ejkck.setUserId(Math.toIntExact(loginUser.getUserId())); //用户ID
        ejkck.setDeptId(Math.toIntExact(loginUser.getDeptId())); //部门ID
        ejkck.setCreateBy(loginUser.getUsername()); //创建人
        ejkck.setIdWhere("存在");
        int rows = ejkckMapper.insertEjkck(ejkck);
        insertEjkckCkmx(ejkck);
        return rows;
    }

    /**
     * 批量新增二级库出库
     *
     * @param ejkcks 二级库出库List
     * @return 结果
     */
    @Override
    public int batchInsertEjkck(List<Ejkck> ejkcks) {
        SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH, false);
        int count = 0;
        if (!CollectionUtils.isEmpty(ejkcks)) {
            try {
                for (int i = 0; i < ejkcks.size(); i++) {
                    int row = ejkckMapper.insertEjkck(ejkcks.get(i));
                    // 防止内存溢出，每100次提交一次,并清除缓存
                    boolean bool = (i > 0 && i % 100 == 0) || i == ejkcks.size() - 1;
                    if (bool) {
                        sqlSession.commit();
                        sqlSession.clearCache();
                    }
                    count = i + 1;
                }
            } catch (Exception e) {
                e.printStackTrace();
                // 没有提交的数据可以回滚
                sqlSession.rollback();
            } finally {
                sqlSession.close();
                return count;
            }
        }
        return count;
    }

    /**
     * 修改二级库出库
     *
     * @param ejkck 二级库出库
     * @return 结果
     */
    @Transactional
    @Override
    public int updateEjkck(Ejkck ejkck) {
        ejkck.setUpdateTime(DateUtils.getNowDate());
        ejkckMapper.deleteEjkckCkmxByEjkckId(ejkck.getEjkckId());
        insertEjkckCkmx(ejkck);
        return ejkckMapper.updateEjkck(ejkck);
    }

    /**
     * 批量删除二级库出库
     *
     * @param ejkckIds 需要删除的二级库出库主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteEjkckByEjkckIds(String[] ejkckIds) {
        ejkckMapper.deleteEjkckCkmxByEjkckIds(ejkckIds);
        return ejkckMapper.deleteEjkckByEjkckIds(ejkckIds);
    }

    /**
     * 删除二级库出库信息
     *
     * @param ejkckId 二级库出库主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteEjkckByEjkckId(String ejkckId) {
        ejkckMapper.deleteEjkckCkmxByEjkckId(ejkckId);
        return ejkckMapper.deleteEjkckByEjkckId(ejkckId);
    }

    /**
     * 新增出库明细信息
     *
     * @param ejkck 二级库出库对象
     */
    public void insertEjkckCkmx(Ejkck ejkck) {
        List<EjkckCkmx> ejkckCkmxList = ejkck.getEjkckCkmxList();
        String ejkckId = ejkck.getEjkckId();
        if (StringUtils.isNotNull(ejkckCkmxList)) {
            List<EjkckCkmx> list = new ArrayList<EjkckCkmx>();
            for (EjkckCkmx ejkckCkmx : ejkckCkmxList) {
                ejkckCkmx.setEjkckId(ejkckId);
                list.add(ejkckCkmx);
            }
            if (list.size() > 0) {
                ejkckMapper.batchEjkckCkmx(list);
            }
        }
    }
}
